from sanic import Sanic
import aiomysql
import aioredis
from sanic import response
import asyncio
import os
import requests
import json
from . import app_user_update
from db_connect.conn_mysql import ConnMysql
from db_connect.conn_redis import ConnRedis
from db_connect.conn_slave_mysql import ConnMysqlSlave

@app_user_update.route("/user/store_registration/<ID:int>",methods=['POST'])
async def user(request,ID):

    if not request.method == 'POST':
    
        return response.json({
                'status': -1,
                'errorCode': 1,
                'errorMsg':f"请求方式error,request.method:{request.method}",
                'resultBody':None
            })

    user_info = request.form
    img_name = user_info.get("img_name")
    user_name = user_info.get("user_name")
    user_site = user_info.get("user_site")

    user_heading_site = request.files.get("user_heading_site")

    with open("static/head_img/{}.jpg".format(img_name),"wb") as img:
        img.write(user_heading_site.body)


    if not all([user_name,user_site]):
    
        return response.json({
                'status': -1,
                'errorCode': 3,
                'errorMsg':f"缺少参数user_name:{user_name},user_site:{user_site}",
                'resultBody':None
            })
    async with ConnMysql().pool.acquire() as c:
        async with c.cursor(aiomysql.DictCursor) as cur:

            sql = 'UPDATE user SET user_head_img = %s,user_name = %s,user_site = %s WHERE id = %s'

            await cur.execute(sql,("/static/heading_img/头像.jpg",user_name,user_site,ID))
            await c.commit()

            return response.json({
                'status': 1,
                'errorCode': '',
                'errorMsg':"",
                'resultBody':"FINISH"
            })





